package com.example.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ChenXiaoBin
 * Redis初始化配置
 * on 2020-03-06
 */
@Configuration
@EnableConfigurationProperties({Redis.class})
public class RedissionConfig {
    private Redis properties;

    @Autowired
    public RedissionConfig(Redis properties, WxPayProperties wxPayProperties) {
        this.properties = properties;
    }

    @Bean
    public RedissonClient getRedisClient() {
        String host = StringUtils.trimToNull(this.properties.getHost());

        String port = StringUtils.trimToNull(this.properties.getPort());

        String password = StringUtils.trimToNull(this.properties.getPassword());

        String address = "redis://" + host + ":" + port;
        Config config = new Config();
        config.useSingleServer().setAddress(address).setPassword(password);
        return Redisson.create(config);
    }
}
